/**
 * Created by oureda on 4/23/16.
 */
var db = require("./index");
var co = require("co");
var client = require("socket.io-client");
var Promise = require("bluebird");
var Data = db.models.Data;
var User = db.models.User;
var Device = db.models.Device;

function * DeviceSeed(){
    console.log("Device Seed")

    yield Device.create({
        name : "Intel"
    });
}

function * UserSeed(){
    console.log("User seed");
    var intel = yield Device.findOne({});

    yield User.create({
        phone: "18340861711",
        pwd : "123456",
        type : 1,
        DeviceID : intel.id
    });
}

/**
 * 传感器数据初始化
 */
function * SensorSeed(){
    console.log("Sensor seed");
    var device = yield Device.findOne({});

    for(var i = 0;i < 100;i ++){
        yield Data.create({
            value : 10 + parseInt(Math.random() * 10),
            type : "temperature",
            equipment : "intelSensor",
            DeviceID : device.id
        });
    }

    for(i = 0;i < 100;i ++){
        yield Data.create({
            value : 10 + parseInt(Math.random() * 10),
            type : "humidity",
            equipment : "intelSensor",
            DeviceID : device.id
        });
    }
}

/**
 * 人体感应数据初始化
 */
function * BodySeed(){
    console.log("BodySeed");
    var device = yield Device.findOne({});

    yield Data.create({
        value : "0",
        type : "state",
        equipment : "intelBody",
        DeviceID : device.id
    });
}

/**
 * 空调温度和风速初始化
 */
function * AirSeed(){
    console.log("AirSeed");
    var device = yield Device.findOne({});

    yield Data.create({
        value : "20",
        type : "temperature",
        equipment : "intelAir",
        DeviceID : device.id
    });

    yield Data.create({
        value : "10",
        type : "wind",
        equipment : "intelAir",
        DeviceID : device.id
    });
}

/**
 * 插线板开关数据初始化
 */
function * PatchSeed(){
    console.log("PatchSeed");
    var device = yield Device.findOne({});

    yield Data.create({
        value : "000",
        type : "state",
        equipment : "intelPatch",
        DeviceID : device.id
    });
}

/**
 * 门铃初始化
 */
function * DoorBellSeed(){
    console.log("DoorBellSeed");
    var device = yield Device.findOne({});

    yield Data.create({
        value : "{ name : 'bian' }",
        type : "json",
        equipment : "intelDoorBell",
        DeviceID : device.id
    });
}

function * PlanSeed(){
    console.log("PlanSeed");
    var device = yield Device.findOne({});

    var bodyPlan = {
        target : "intelPatch",
        result : {
            on : "111",
            off : "000"
        }
    };

    yield Data.create({
        equipment : "intelBody",
        type : "plan",
        value : JSON.stringify(bodyPlan),
        DeviceID : device.id
    });
}

function * init(){
    console.log("initing");
    yield db.sync({force:true});
    yield DeviceSeed();
    yield UserSeed();
    yield SensorSeed();
    yield BodySeed();
    yield AirSeed();
    yield PatchSeed();
    yield DoorBellSeed();
    yield PlanSeed();
}

var isPro = process.argv[2] === "--production";

co(function *(){
    if(!isPro){
        yield init();
    }
    console.log("finished");
    process.exit(0);
}).catch((err) => {
    console.log(err.stack);
});
